package cn.iocoder.yudao.module.def.controller.admin.equipmentinfo;

import io.swagger.v3.oas.annotations.Parameters;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.annotation.security.PermitAll;
import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.def.controller.admin.equipmentinfo.vo.*;
import cn.iocoder.yudao.module.def.dal.dataobject.equipmentinfo.EquipmentInfoDO;
import cn.iocoder.yudao.module.def.service.equipmentinfo.EquipmentInfoService;
import org.springframework.web.multipart.MultipartFile;

@Tag(name = "管理后台 - 设备信息")
@RestController
@RequestMapping("/def/equipment-info")
@Validated
public class EquipmentInfoController {

    @Resource
    private EquipmentInfoService equipmentInfoService;

    @PostMapping("/create")
    @Operation(summary = "创建设备信息")
    @PreAuthorize("@ss.hasPermission('def:equipment-info:create')")
    public CommonResult<Long> createEquipmentInfo(@Valid @RequestBody EquipmentInfoSaveReqVO createReqVO) {
        return success(equipmentInfoService.createEquipmentInfo(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新设备信息")
    @PreAuthorize("@ss.hasPermission('def:equipment-info:update')")
    public CommonResult<Boolean> updateEquipmentInfo(@Valid @RequestBody EquipmentInfoSaveReqVO updateReqVO) {
        equipmentInfoService.updateEquipmentInfo(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除设备信息")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('def:equipment-info:delete')")
    public CommonResult<Boolean> deleteEquipmentInfo(@RequestParam("id") Long id) {
        equipmentInfoService.deleteEquipmentInfo(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得设备信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('def:equipment-info:query')")
    public CommonResult<EquipmentInfoRespVO> getEquipmentInfo(@RequestParam("id") Long id) {
        EquipmentInfoDO equipmentInfo = equipmentInfoService.getEquipmentInfo(id);
        return success(BeanUtils.toBean(equipmentInfo, EquipmentInfoRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得设备信息分页")
    @PreAuthorize("@ss.hasPermission('def:equipment-info:query')")
    public CommonResult<PageResult<EquipmentInfoRespVO>> getEquipmentInfoPage(@Valid EquipmentInfoPageReqVO pageReqVO) {
        PageResult<EquipmentInfoDO> pageResult = equipmentInfoService.getEquipmentInfoPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, EquipmentInfoRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出设备信息 Excel")
    @PreAuthorize("@ss.hasPermission('def:equipment-info:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportEquipmentInfoExcel(@Valid EquipmentInfoPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<EquipmentInfoDO> list = equipmentInfoService.getEquipmentInfoPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "设备信息.xls", "数据", EquipmentInfoRespVO.class,
                        BeanUtils.toBean(list, EquipmentInfoRespVO.class));
    }


    @PostMapping("/import")
    @Operation(summary = "导入数据")
    @Parameters({
            @Parameter(name = "file", description = "Excel 文件", required = true)
    })
    @PermitAll
    public CommonResult<Object> importExcel(@RequestParam("file") MultipartFile file) throws IOException {
        equipmentInfoService.dealImportFile(file);
        return success(true);
    }

    @GetMapping("/export")
    public void exportEquipment(HttpServletResponse response) throws IOException {
        equipmentInfoService.exportMultiSheetEquipment(response);
    }
}